package cn.com.infcn.spider.domain;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jsoup.nodes.Document;
import org.python.jline.internal.Log;

import cn.com.infcn.spider.util.ContentExtractor;
import cn.com.infcn.spider.util.ObjConver;
import cn.com.infcn.spider.util.URLUtil;

public class Page extends Doc {

	private String url = null;

	private String html = null;

	private Document document = null;

	private boolean ok;

	private int deep; // 深度

	private int fromUrl; // 来源网址

	public boolean skipSave = false; // 是否输出,默認爲輸出

	private boolean skip = false; // 是否结束当前进程

	private String nextPage;

	private Set<String> urls = new HashSet<>();

	private List<String> regexSkipUrls = new ArrayList<>();

	private List<String> inDbSkipUrls = new ArrayList<>();

	private List<String> addUrls = new ArrayList<>();

	private List<RegexDomain> regexList = null;

	private boolean useProxy = false;

	public int getDeep() {
		return deep;
	}

	public void setDeep(int deep) {
		this.deep = deep;
	}

	public int getFromUrl() {
		return fromUrl;
	}

	public void setFromUrl(int fromUrl) {
		this.fromUrl = fromUrl;
	}

	public void setSkipSave(boolean skipSave) {
		this.skipSave = skipSave;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	public boolean getSkipSave() {
		return skipSave;
	}

	public void setSkipSave(Boolean skipSave) {
		if (skipSave == null) {
			skipSave = true;
		}
		this.skipSave = skipSave;
	}

	public boolean getSkip() {
		return skip;
	}

	public void setSkip(Boolean skip) {
		if (skip == null) {
			skip = false;
		}
		this.skip = skip;
	}

	public String getNextPage() {
		return nextPage;
	}

	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}

	public List<String> getRegexSkipUrls() {
		return regexSkipUrls;
	}

	public void setRegexSkipUrls(List<String> regexSkipUrls) {
		this.regexSkipUrls = regexSkipUrls;
	}

	public List<String> getInDbSkipUrls() {
		return inDbSkipUrls;
	}

	public void setInDbSkipUrls(List<String> inDbSkipUrls) {
		this.inDbSkipUrls = inDbSkipUrls;
	}

	public List<String> getAddUrls() {
		return addUrls;
	}

	public void setAddUrls(List<String> addUrls) {
		this.addUrls = addUrls;
	}

	public Set<String> getUrls() {
		return urls;
	}

	public void setUrls(Set<String> urls) {
		this.urls = urls;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
		super.setId(url);
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public boolean isOk() {
		return ok;
	}

	public void setOk(boolean ok) {
		this.ok = ok;
	}

	public void setRegexList(List<RegexDomain> regexList) {
		this.regexList = regexList;
	}

	/**
	 * 设置下一页
	 * 
	 * @param param
	 * @param step
	 */
	public void nextPage(String param, int step) {
		this.nextPage = URLUtil.nextPage(this.url, param, step);
	}
	
	
	
	
	/**
	 * 设置下一页
	 * 
	 * @param param
	 * @param step
	 */
	public void nextPageByTemplate(String template, int step) {
		this.nextPage = URLUtil.nextPageByTemplate(this.url, template, step);
	}

	/**
	 * 设置下一页
	 * 
	 * @param param
	 * @param step
	 */
	public void nextPage(int index, int step) {
		this.nextPage = URLUtil.nextPage(this.url, index, step);
	}

	/**
	 * 抽取url地址
	 * 
	 * @param cssQuery
	 */
	public void parseUrls(String cssQuery) {
		this.urls.addAll(URLUtil.urls(this.document, cssQuery));
	}

	public void parseUrls() {
		this.urls.addAll(URLUtil.urls(this.document, null));
	}

	public void setValue(String key, String cssQuery) {
		setValue(key, cssQuery, null);
	}

	/**
	 * 设置参数到result
	 * 
	 * @param cssQuery
	 * @param type
	 */
	public void setValue(String key, String cssQuery, String type) {
		String valueStr = URLUtil.extracting(this.document, cssQuery);

		if (type == null) {
			type = "string";
		}

		Object value = valueStr;

		switch (type.toLowerCase()) {
		case "data":
			value = ObjConver.castToDate(value);
			break;
		case "double":
			value = ObjConver.castToDouble(value);
			break;
		case "int":
		case "integer":
			value = ObjConver.castToInt(value);
			break;
		case "long":
			value = ObjConver.castToLong(value);
			break;
		case "float":
			value = ObjConver.castToFloat(value);
			break;
		}

		this.getResult().put(key, value);
	}

	public boolean isUseProxy() {
		return useProxy;
	}

	public void setUseProxy(boolean useProxy) {
		this.useProxy = useProxy;
	}

	/**
	 * 判断网址是否正确
	 * 
	 * @param regexId
	 * @return
	 */
	public boolean caseUrl(String regexId) {
		RegexDomain regex = null;
		for (RegexDomain rd : regexList) {
			if (rd.getName().equals(regexId)) {
				regex = rd;
				break;
			}
		}

		return URLUtil.matches(url, regex.getRegex());
	}

	/**
	 * 作为新闻页抽取
	 */
	public void makeNews() {
		try {
			News news = ContentExtractor.getNewsByDoc(this.document);
			getResult().put("title", news.getTitle());
			getResult().put("content", news.getContent());
			getResult().put("publish_time", news.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			Log.error(e);
		}
	}

}
